package com.sam;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Menu
 */
public class Menu extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String LIST_MEMBER = "lst_member.jsp";
    private static final String ADD_MEMBER = "add_member.jsp";
    private static final String EDIT_MEMBER = "edit_member.jsp";
    
    //private static final String ADD_POST = "add_post.jsp";
    
    private MemberDAO dao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Menu() {
        super();
        dao = new MemberDAO();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=null;
		String forward=LIST_MEMBER;
		
		action = request.getParameter("action");
		System.out.println("Servlet menu action = " + action);
		
		if (action.equalsIgnoreCase("lst_member")) {
			forward = LIST_MEMBER;
			HttpSession session = request.getSession();
			session.setAttribute("center", "lst_member.jsp");
			System.out.println("lst_member forward = " + forward);
		}
		
		// action is add_member
		if (action.equalsIgnoreCase("add_member")) {
			forward = ADD_MEMBER;
			
			// check userid is not null
			if (request.getParameter("userid") != null) {
				String userid = request.getParameter("userid");
				String username = request.getParameter("username");
				String nickname = request.getParameter("nickname");
				String password = request.getParameter("password");
				String email = request.getParameter("email");
				String status = " ";
				                    
				Member m = new Member(userid, username, nickname, password, email, status);
				dao.add(m);
				
				forward = LIST_MEMBER;
			} else {
				forward = ADD_MEMBER;
			}
		}
		
		// action is edit_member
		if (action.equalsIgnoreCase("edit_member")) {
					
			System.out.println("edit_member userid = " + dao.getMemberById(request.getParameter("userid")));
					
			Member m = dao.getMemberById(request.getParameter("userid"));
			request.setAttribute("m", m);
			forward = LIST_MEMBER;
		}
		// update all members data
		List<Member> members = dao.getAll();
		request.setAttribute("members", members);
		System.out.println("last forward = " + forward);
					
		RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
		dispatcher.forward(request, response);
		
	}
			
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}	
}
